<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('训练任务列表')"/>
    <script src="/sweetalert2-11.12.2/sweetalert2@11.js"></script>
    <style>
        .progress {
            margin-top: 18px;
        }
        .status-btn {
            display: inline-block;
            padding: 1px 20px;
            margin: 0px;
            margin-top: 5px;
            background-color: #5bc0de; /* 可以根据需要调整颜色 */
            color: white;
            text-decoration: none;
            border-radius: 5px;
            transition: background-color 0.3s;
        }

        .status-btn:hover {
            background-color: #31b0d5; /* 悬停时的颜色变化 */
        }
        /*按钮下间距*/
        .button-spacing {
            margin-bottom: 5px; /* 根据需要调整 */
        }
    </style>
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="formId">
                <div class="select-list">
                    <ul>
                        <li>
                            模型：<input type="text" name="model"/>
                        </li>

                        <li>
                            训练数据集：<input type="text" name="datasetTrain"/>
                        </li>

                        <li>
                            验证数据集：<input type="text" name="datasetVal"/>
                        </li>

                        <li>
                            训练状态：<input type="text" name="status"/>
                        </li>
                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i
                                    class="fa fa-search"></i>&nbsp;搜索</a>
                            <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i
                                    class="fa fa-refresh"></i>&nbsp;重置</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>

        <div class="btn-group-sm" id="toolbar" role="group">
            <a class="btn btn-success" onclick="$.operate.add()" shiro:hasPermission="deep_learning:trainMission:add">
                <i class="fa fa-plus"></i> 添加
            </a>
            <li>
                <!--                通过点击按钮来切换显示的数据集状态-->
                <a class="status-btn" data-status="self">仅自己</a>
                <a class="status-btn" data-status="all">全部</a>
            </li>
        </div>
        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table" class="table table-bordered" data-mobile-responsive="true"></table>
        </div>
    </div>
</div>
<div th:include="include :: footer"></div>
<script th:inline="javascript">
    var editFlag = [[${@permission.hasPermi('deep_learning:trainMission:edit')}]];
    var removeFlag = [[${@permission.hasPermi('deep_learning:trainMission:remove')}]];
    var prefix = ctx + "deep_learning/trainMission";
    var currentUserId = null;
    var options=null;
    var optionsAdmin= null;
    var tableStatus='仅自己';
    var gprogress = {};

    // 根据不同的请求来刷新
    function refreshWithOptions(url) {

        //普通用户显示的表格
        options = {
            // url: prefix + "/list",
            // url: prefix + "/listWithUser",
            // url: prefix + "/listWithUserOrPublic",
            url:url,
            createUrl: prefix + "/add",
            updateUrl: prefix + "/edit/{id}",
            removeUrl: prefix + "/remove",
            exportUrl: prefix + "/export",
            modalName: "训练任务",
            showExport: true,
            // sidePagination: 'client', // 前端分页
            columns: [
                {
                    field: 'id',
                    title: 'id',
                    visible: false
                },
                {
                    field : 'trainMissionName',
                    title : '训练任务名称',
                    align: 'center',
                    sortable: false,
                    formatter: function(value, row, index) {
                        return formatModelName(value, row.id); // 调用自定义的格式化函数
                    }
                },
                {
                    field: 'userName',
                    title: '归属用户',
                    align: 'center',
                    sortable: true
                },
                {
                    field: 'model',
                    title: '模型',
                    align: 'center',
                    sortable: true
                },
                {
                    field: 'datasetTrain',
                    title: '训练数据集',
                    align: 'center',
                    sortable: true
                },
                {
                    field: 'datasetVal',
                    title: '验证数据集',
                    align: 'center',
                    sortable: true
                },
                {
                    field: 'batchSize',
                    title: 'batch_size',
                    align: 'center',
                    sortable: true
                },
                {
                    field: 'minEpoch',
                    title: '最小训练轮数',
                    align: 'center',
                    sortable: true
                },
                {
                    field: 'maxEpoch',
                    title: '最大训练轮数',
                    align: 'center',
                    sortable: true
                },
                {
                    field: 'status',
                    title: '训练状态',
                    align: 'center',
                    formatter: function (value, row, index) {
                        var statusText = '';
                        var color = '';
                        var buttonHtml = '';

                        switch (value) {
                            case '未训练':
                                statusText = '未训练';
                                color = 'gray';
                                break;
                            case '待训练':
                                statusText = '待训练';
                                color = 'orange';
                                break;
                            case '训练中':
                                statusText = '训练中';
                                color = 'blue';
                                break;
                            case '已完成':
                                statusText = '已完成';
                                color = 'green';
                                break;
                            default:
                                statusText = '训练异常';
                                color = 'black';
                                buttonHtml = '<button onclick="viewExceptionLog(\'' + row.id + '\')">查看</button>'; // 添加查看按钮
                        }
                        return '<span style="color: ' + color + ';">' + statusText + '</span>' + buttonHtml;
                    },
                    sortable: true
                },
                {
                    field: 'earlyStop',
                    title: '早停',
                    align: 'center',
                    sortable: true
                },
                {
                    field: 'opLr',
                    title: '学习率',
                    align: 'center',
                    sortable: true
                },
                {
                    //     进度条
                    field: 'progress',
                    title: '进度',
                    align: 'center',
                    formatter: function (value, row, index) {
                        // 默认值，如果为空
                        var progressValue = value || 0.0;

                        // 创建一个带有 data-sort 属性的容器
                        var progressBarContainer = '<div id="progress-bar-' + row.id + '" class="progress progress-striped active" data-sort="' + progressValue + '">' +
                            '<div class="progress-bar progress-bar-success" role="progressbar" aria-valuenow="' + progressValue + '" aria-valuemin="0" aria-valuemax="100" style="width: ' + progressValue + '%;">' +
                            progressValue + '%' +
                            '</div>' +
                            '</div>';

                        // 定义一个函数来更新进度条
                        function updateProgressBar() {
                            $.ajax({
                                url: '/deep_learning/trainMission/getProgress/' + row.id, // 根据行ID获取进度数据的API地址
                                method: 'GET',
                                success: function (progressData) {
                                    if (progressData.status === "success") {
                                        var newValue = progressData.data;
                                        $('#progress-bar-' + row.id + ' .progress-bar').css('width', newValue + '%').css('color','black').attr('aria-valuenow', newValue).text(newValue + '%');
                                        $('#progress-bar-' + row.id).attr('data-sort', newValue); // 更新 data-sort 属性

                                        // 如果进度未达到 100%，继续定期轮询
                                        if (newValue < 100) {
                                            setTimeout(updateProgressBar, 1000); // 每5秒轮询一次
                                        }
                                        row.progress=newValue;
                                    } else {
                                        console.error('Failed to fetch progress data:', progressData.message);
                                    }
                                },
                                error: function (xhr, status, error) {
                                    console.error('Failed to fetch progress data:', error);
                                }
                            });
                        }

                        // 初次调用更新进度条
                        updateProgressBar();

                        return progressBarContainer;
                    }
                    ,
                    sortable: false
                },
                {
                    field: 'beginTime',
                    title: '开始时间',
                    align: 'center',
                    sortable: true
                },
                {
                    field: 'endTime',
                    title: '结束时间',
                    align: 'center',
                    sortable: true
                },   {
                    field: 'queuePosition',
                    title: '队列位次',
                    align: 'center',
                    sortable: true,
                    formatter: function (value, row, index) {
                        // 默认值，如果为空则设为-1，这样可以正确排序
                        var queuePositionValue = value || Infinity;

                        // 创建一个带有 data-sort 属性的容器
                        var queuePositionContainer = '<div id="queue-position-' + row.id + '" data-sort="' + queuePositionValue + '">加载中...</div>';

                        // 定义一个函数来更新队列位次
                        function updateQueuePosition() {
                            $.ajax({
                                url: '/deep_learning/trainMission/getQueuePosition/' + row.id,  // 使用GET请求并将row.id作为路径参数传递
                                method: 'GET',
                                success: function (response) {
                                    if (response.queuePosition !== null) {
                                        var newQueuePosition = response.queuePosition;
                                        // 更新显示内容和 data-sort 属性
                                        $('#queue-position-' + row.id).text(newQueuePosition).attr('data-sort', newQueuePosition);
                                        row.queuePosition=newQueuePosition;
                                    } else {
                                        // 如果返回的是null，则显示空值并设置排序值为-1
                                        $('#queue-position-' + row.id).text('-').attr('data-sort', Infinity);
                                    }
                                },
                                error: function (xhr, status, error) {
                                    console.error('Failed to fetch queue position:', error);
                                    $('#queue-position-' + row.id).text('错误').attr('data-sort', Infinity);
                                }
                            });
                        }

                        // 初次调用更新队列位次
                        updateQueuePosition();

                        return queuePositionContainer;
                    }

                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function (value, row, index) {
                        if (row.userId === currentUserId) {
                            var actions = [];
                            var startButton = '<a class="btn btn-success btn-xs button-spacing" href="javascript:void(0)" data-id="' + row.id + '" onclick="$.operate.startSingle(\'' + row.id + '\')" style="background-color: gray; border-color: gray;"><i class="fa fa-play" style="color: green;"></i> 启动</a>';
                            var stopButton = '<a class="btn btn-danger btn-xs button-spacing" href="javascript:void(0)" data-id="' + row.id + '" onclick="$.operate.stopSingle(\'' + row.id + '\')" style="background-color: white; border-color: red; color: red;"><i class="fa fa-stop" style="color: red;"></i> 终止</a>';
                            var editButton = '<a class="btn btn-success btn-xs button-spacing ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.id + '\')"><i class="fa fa-edit"></i>编辑</a>';
                            var removeButton = '<a class="btn btn-danger btn-xs button-spacing ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.removeWithTrainingFile(\'' + row.id + '\')"><i class="fa fa-remove"></i>删除</a>';
                            // 添加一个加号按钮，用于将训练任务添加到任务列表
                            var addButton = '<a class="btn btn-primary btn-xs button-spacing add-task" href="javascript:void(0)" data-id="' + row.id + '" onclick="$.operate.addToTrainingList(this, \'' + row.id + '\')"><i class="fa fa-plus"></i> 添加到训练队列</a>';
                            var removeTrainingButton = '<a class="btn btn-danger btn-xs button-spacing remove-task" href="javascript:void(0)" data-id="' + row.id + '" onclick="$.operate.removeFromTrainingList(this, \'' + row.id + '\')"><i class="fa fa-minus"></i> 移除训练</a>';
                            // 如果任务是未训练状态，显示加号按钮，如果是待训练状态，显示移除训练按钮
                            if (row.status === "未训练") {
                                actions.push(addButton)
                                actions.push(startButton)
                            } else if (row.status === "待训练") {
                                actions.push(removeTrainingButton)
                            }
                            if (row.status === "训练中") {
                                // 如果任务正在运行，显示终止按钮
                                actions.push(stopButton);
                            } else {
                                // 任务未运行或已完成，显示启动按钮
                                // actions.push(startButton);
                            }
                            actions.push(editButton);
                            actions.push(removeButton);
                            // actions.push('<a class="btn btn-info btn-xs button-spacing" href="javascript:void(0)" onclick="$.operate.viewTrainingResult(\'' + row.id + '\')"><i class="fa fa-eye"></i>查看</a>'); // 新增的“查看”按钮
                            if (row.beginTime!==null){
                                actions.push('<a class="btn btn-info btn-xs button-spacing" href="javascript:void(0)" onclick="$.operate.viewTrainingPictureResult(\'' + row.id + '\')"><i class="fa fa-eye"></i>查看</a>'); // 新增的“查看”按钮
                            }
                            // console.log(row.userId)
                            return actions.join(' ');}

                    }
                }]
        };
        //管理员显示的表
        optionsAdmin = {
            // url: prefix + "/list",
            // url: prefix + "/listWithUser",
            url:url,
            createUrl: prefix + "/add",
            updateUrl: prefix + "/edit/{id}",
            removeUrl: prefix + "/remove",
            exportUrl: prefix + "/export",
            modalName: "训练任务",
            showExport: true,
            // sidePagination: 'client', // 前端分页
            columns: [
                {
                    field: 'id',
                    title: 'id',
                    visible: false
                },
                {
                    field : 'trainMissionName',
                    title : '训练任务名称',
                    align: 'center',
                    sortable: true,
                    formatter: function(value, row, index) {
                        return formatModelName(value, row.id); // 调用自定义的格式化函数
                    }
                },
                {
                    field: 'userName', // 改为 userName
                    title: '归属用户',
                    align: 'center',
                    sortable: true
                },
                {
                    field: 'model',
                    title: '模型',
                    align: 'center',
                    sortable: true
                },
                {
                    field: 'datasetTrain',
                    title: '训练数据集',
                    align: 'center',
                    sortable: true
                },
                {
                    field: 'datasetVal',
                    title: '验证数据集',
                    align: 'center',
                    sortable: true
                },
                {
                    field: 'batchSize',
                    title: 'batch_size',
                    align: 'center',
                    sortable: true
                },
                {
                    field: 'minEpoch',
                    title: '最小训练轮数',
                    align: 'center',
                    sortable: true
                },
                {
                    field: 'maxEpoch',
                    title: '最大训练轮数',
                    align: 'center',
                    sortable: true
                },
                {
                    field: 'status',
                    title: '训练状态',
                    align: 'center',
                    formatter: function (value, row, index) {
                        var statusText = '';
                        var color = '';
                        var buttonHtml = '';

                        switch (value) {
                            case '未训练':
                                statusText = '未训练';
                                color = 'gray';
                                break;
                            case '待训练':
                                statusText = '待训练';
                                color = 'orange';
                                break;
                            case '训练中':
                                statusText = '训练中';
                                color = 'blue';
                                break;
                            case '已完成':
                                statusText = '已完成';
                                color = 'green';
                                break;
                            default:
                                statusText = '训练异常';
                                color = 'black';
                                buttonHtml = '<button onclick="viewExceptionLog(\'' + row.id + '\')">查看</button>'; // 添加查看按钮
                        }
                        return '<span style="color: ' + color + ';">' + statusText + '</span>' + buttonHtml;
                    },
                    sortable: true
                },
                {
                    field: 'earlyStop',
                    title: '早停',
                    align: 'center',
                    sortable: true
                },
                {
                    field: 'opLr',
                    title: '学习率',
                    align: 'center',
                    sortable: true
                },
                {
                    //     进度条
                    field: 'progress',
                    title: '进度',
                    align: 'center',
                    sortable: true,
                    // 自定义排序函数
                    formatter: function (value, row, ) {
                        // 默认值，如果为空
                        // console.log("value",value)
                        var progressValue = parseFloat(value) || 0.0;
                        // console.log("progressValue",progressValue)
                        // 创建一个带有 data-sort 属性的容器
                        var progressBarContainer = '<div id="progress-bar-' + row.id + '" class="progress progress-striped active" data-sort="' + progressValue + '">' +
                            '<div class="progress-bar progress-bar-success" role="progressbar" aria-valuenow="' + progressValue + '" aria-valuemin="0" aria-valuemax="100" style="width: ' + progressValue + '%;">' +
                            progressValue + '%' +
                            '</div>' +
                            '</div>';

                        // 定义一个函数来更新进度条
                        function updateProgressBar() {
                            $.ajax({
                                url: '/deep_learning/trainMission/getProgress/' + row.id, // 根据行ID获取进度数据的API地址
                                method: 'GET',
                                success: function (progressData) {
                                    if (progressData.status === "success") {
                                        var newValue = parseFloat(progressData.data);
                                        $('#progress-bar-' + row.id + ' .progress-bar').css('width', newValue + '%').css('color','black').attr('aria-valuenow', newValue).text(newValue + '%');
                                        $('#progress-bar-' + row.id).attr('data-sort', newValue); // 更新 data-sort 属性
                                        // console.log("值为："+$('#progress-bar-' + row.id).attr('data-sort'))
                                        // 如果进度未达到 100%，继续定期轮询
                                        if (newValue < 100) {
                                            setTimeout(updateProgressBar, 1000); // 每1秒轮询一次
                                        }
                                        row.progress=newValue;
                                    } else {
                                        console.error('Failed to fetch progress data:', progressData.message);
                                    }
                                },
                                error: function (xhr, status, error) {
                                    console.error('Failed to fetch progress data:', error);
                                }
                            });
                        }
                        // 初次调用更新进度条
                        updateProgressBar();
                        console.log("progressBarContainer"+$('#progress-bar-' + row.id).attr('data-sort'))
                        return progressBarContainer;
                    },
                },
                {
                    field: 'beginTime',
                    title: '开始时间',
                    align: 'center',
                    sortable: true
                },
                {
                    field: 'endTime',
                    title: '结束时间',
                    align: 'center',
                    sortable: true
                },
                // {
                //     field : 'userId',
                //     title : '归属用户',
                //     align: 'center'
                // },

                {
                    field: 'queuePosition',
                    title: '队列位次',
                    align: 'center',
                    sortable: true,
                    // sorter: function (a, b) {
                    //     return a - b;
                    // },
                    formatter: function (value, row, index) {
                        // 默认值，如果为空则设为-1，这样可以正确排序
                        var queuePositionValue = value || Infinity;

                        // 创建一个带有 data-sort 属性的容器
                        var queuePositionContainer = '<div id="queue-position-' + row.id + '" data-sort="' + queuePositionValue + '">加载中...</div>';

                        // 定义一个函数来更新队列位次
                        function updateQueuePosition() {
                            $.ajax({
                                url: '/deep_learning/trainMission/getQueuePosition/' + row.id,  // 使用GET请求并将row.id作为路径参数传递
                                method: 'GET',
                                success: function (response) {
                                    if (response.queuePosition !== null) {
                                        var newQueuePosition = response.queuePosition;
                                        // 更新显示内容和 data-sort 属性
                                        $('#queue-position-' + row.id).text(newQueuePosition).attr('data-sort', newQueuePosition);
                                        row.queuePosition=newQueuePosition;
                                    } else {
                                        // 如果返回的是null，则显示空值并设置排序值为-1
                                        $('#queue-position-' + row.id).text('-').attr('data-sort', Infinity);
                                    }
                                },
                                error: function (xhr, status, error) {
                                    console.error('Failed to fetch queue position:', error);
                                    $('#queue-position-' + row.id).text('错误').attr('data-sort', Infinity);
                                }
                            });
                        }

                        // 初次调用更新队列位次
                        updateQueuePosition();

                        return queuePositionContainer;
                    }

                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function (value, row, index) {
                        var actions = [];
                        var startButton = '<a class="btn btn-success btn-xs button-spacing" href="javascript:void(0)" data-id="' + row.id + '" onclick="$.operate.startSingle(\'' + row.id + '\')" style="background-color: gray; border-color: gray;"><i class="fa fa-play" style="color: green;"></i> 启动</a>';
                        var stopButton = '<a class="btn btn-danger btn-xs button-spacing" href="javascript:void(0)" data-id="' + row.id + '" onclick="$.operate.stopSingle(\'' + row.id + '\')" style="background-color: white; border-color: red; color: red;"><i class="fa fa-stop" style="color: red;"></i> 终止</a>';
                        var editButton = '<a class="btn btn-success btn-xs button-spacing ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.id + '\')"><i class="fa fa-edit"></i>编辑</a>';
                        var removeButton = '<a class="btn btn-danger btn-xs button-spacing ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.removeWithTrainingFile(\'' + row.id + '\')"><i class="fa fa-remove"></i>删除</a>';
                        // 添加一个加号按钮，用于将训练任务添加到任务列表
                        var addButton = '<a class="btn btn-primary btn-xs button-spacing add-task" href="javascript:void(0)" data-id="' + row.id + '" onclick="$.operate.addToTrainingList(this, \'' + row.id + '\')"><i class="fa fa-plus"></i> 添加到训练队列</a>';
                        var removeTrainingButton = '<a class="btn btn-danger btn-xs button-spacing remove-task" href="javascript:void(0)" data-id="' + row.id + '" onclick="$.operate.removeFromTrainingList(this, \'' + row.id + '\')"><i class="fa fa-minus"></i> 移除训练</a>';
                        // 如果任务是未训练状态，显示加号按钮，如果是待训练状态，显示移除训练按钮
                        if (row.status === "未训练") {
                            actions.push(addButton)
                            actions.push(startButton)
                        } else if (row.status === "待训练") {
                            actions.push(removeTrainingButton)
                        }
                        if (row.status === "训练中") {
                            // 如果任务正在运行，显示终止按钮
                            actions.push(stopButton);
                        } else {
                            // 任务未运行或已完成，显示启动按钮
                            // actions.push(startButton);
                        }
                        actions.push(editButton);
                        actions.push(removeButton);
                        // actions.push('<a class="btn btn-info btn-xs button-spacing" href="javascript:void(0)" onclick="$.operate.viewTrainingResult(\'' + row.id + '\')"><i class="fa fa-eye"></i>查看</a>'); // 新增的“查看”按钮
                        if (row.beginTime!==null){
                            actions.push('<a class="btn btn-info btn-xs button-spacing" href="javascript:void(0)" onclick="$.operate.viewTrainingPictureResult(\'' + row.id + '\')"><i class="fa fa-eye"></i>查看</a>'); // 新增的“查看”按钮
                        }
                        // console.log(row.progress)
                        // console.log(row.userId)
                        return actions.join(' ');
                    }
                }]
        };

        if (currentUserId!=1){
            $.table.destroy()
            $.table.init(options);
        }else {
            $.table.destroy()
            $.table.init(optionsAdmin);
        }
        // $.table.refresh();
        var refreshButton = $("button[name='refresh']");
        console.log(refreshButton)
        //解除按钮点击事件绑定
        refreshButton.off("click");
        refreshButton.click(function () {
            console.log("refreshButton")
            console.log("tableStatus",tableStatus)
            switch (tableStatus){
                case '仅自己':
                    $.table.destroy();
                    refreshWithOptions(prefix+"/listWithUser")
                    break;
                case '用户全部':
                    $.table.destroy();
                    refreshWithOptions(prefix+"/listWithUserOrPublic")
                    break;
                case '管理员全部':
                    $.table.destroy();
                    refreshWithOptions(prefix+"/list")
                    break;
            }
        });
    }

    $.ajax({
        url: '/deep_learning/app01Dataset/currentUser', // 此地址返回当前用户的 userId
        method: 'GET',
        success: function (response) {
            console.log("response.userId", response.userId)
            currentUserId = response.userId;
        },
        error: function (xhr, status, error) {
            console.error('请求当前用户信息失败:', error);
        }
    }).then(function () {
        console.log("currentUserId", currentUserId)
        // $.table.init(options);
        tableStatus='仅自己';
        refreshWithOptions(prefix+"/listWithUser")

    });


    $(".status-btn").click(function () {
        // 获取按钮上的状态属性
        status = $(this).data("status");
        // console.log("status: " + status);
        if (status === "self") {
            // 仅自己
            console.log("status",status)
            // refreshTableWithOnlyUserId(currentUserId);
            // bindRefreshButton(currentUserId)
            tableStatus='仅自己'
            refreshWithOptions(prefix+"/listWithUser")
            // $.table.init(options)
        } else if (status === "all") {
            // 直接刷新
            if(currentUserId!=1){
                tableStatus='用户全部';
                refreshWithOptions(prefix+"/listWithUserOrPublic")
            }else {
                console.log("点击了管理员全部")
                tableStatus='管理员全部';
                refreshWithOptions(prefix+"/list")
            }
        }
    });

    window.addEventListener('message', function (event) {
        console.log("收到来自子窗口的消息：")
        setTimeout(function () {
            let refreshButton = $("button[name='refresh']");
            refreshButton.click();
            // $.table.refresh();
        }, 80)
    });

    function formatModelName(modelName, modelId) {
        let formattedName = modelName;
        // console.log("modeId",modelId)
        $.ajax({
            url: '/deep_learning/trainMission/getIsWeight/'+modelId,
            type: 'GET',
            async: false, // 设为同步请求，以便函数返回格式化后的名称
            success: function(response) {
                if (response.isWeight) {
                    formattedName = `<strong>${modelName}*</strong>`; // 加星号并加黑字体
                }
            },
            error: function() {
                console.error('请求失败');
            }
        });
        return formattedName;
    }


    function viewExceptionLog(id) {
        // 发送 AJAX 请求以获取异常日志内容
        $.ajax({
            url: '/deep_learning/trainMission/getExceptionLog/' + id,
            method: 'GET',
            success: function (response) {
                console.log( response)
                Swal.fire({
                    title: '训练异常日志',
                    html: `
            <div style="text-align: left;">
                <pre style="white-space: pre-wrap; overflow-y: auto; max-height: 400px; padding: 10px; border: 1px solid #ccc; font-size: 16px;">
                    ${response}
                </pre>
            </div>
        `,
                    confirmButtonText: '关闭',
                    width: '80%',
                    padding: '2em',
                    showCloseButton: true,
                    background: '#fff',
                    backdrop: `rgba(0,0,0,0.4)`
                });
            },
            error: function (xhr, status, error) {
                console.error('Failed to fetch exception log:', error);
                Swal.fire({
                    title: '获取日志失败',
                    text: '请检查网络连接或联系管理员',
                    confirmButtonText: '关闭',
                    width: '30%',
                    padding: '2em',
                    showCloseButton: true,
                    background: '#fff',
                    backdrop: `rgba(0,0,0,0.4)`
                });
            }
        });
    }


    function showModal(logContent) {
        var modalHtml = '<div id="logModal" class="modal" style="display:block;">';
        modalHtml += '<div class="modal-content">';
        modalHtml += '<span class="close" onclick="closeModal()">&times;</span>';
        modalHtml += '<pre>' + logContent + '</pre>'; // 使用 <pre> 保持文本格式
        modalHtml += '</div></div>';

        // 移除已有的模态框
        const existingModal = document.getElementById('logModal');
        if (existingModal) {
            existingModal.remove();
        }

        // 插入新的模态框
        document.body.insertAdjacentHTML('beforeend', modalHtml);
    }

    function closeModal() {
        const modal = document.getElementById('logModal');
        if (modal) {
            modal.remove();
        }
    }

</script>
</body>
</html>